package com.cheng.io.input;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Test:FileInputStream
 *      int read(byte[] b,int off,int len); 一次读取len个字节，并从数组的off下标处开始存储数据
 *      int available(); 返回流中剩余的估计字节数量
 *      long skip(long n); 跳过读取n个数量的字节，返回实际跳过的字节数，可能为零
 */
public class FileInputStreamTest03 {
    public static void main(String[] args) {
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream("D:\\code\\java-study\\java-study\\io\\resources\\test01.txt");
            /*byte[] bytes = new byte[10];
            int read = inputStream.read(bytes, 1, 5);
            System.out.println("read = " + read);
            for (byte b : bytes) {
                System.out.println(b);
            }*/

            /*System.out.println("inputStream.available() = " + inputStream.available());
            byte[] bytes = new byte[inputStream.available()];
            int readCount = inputStream.read(bytes);
            System.out.println(new String(bytes, 0, readCount));*/

            long skipCount = inputStream.skip(2);
            //available()读取的是skip过后的字节数
            int available = inputStream.available();
            byte[] bytes = new byte[available];

            System.out.println("skipCount = " + skipCount);
            int readCount = inputStream.read(bytes);
            System.out.println("readCount = " + readCount);
            System.out.println("read:" + new String(bytes));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
